using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using com.Editors;
using com.Editors.UserControls;

namespace com.code.Editors.Code
{
    public partial class BindTableData : Form
    {
        int numCols = 0;
        ArrayList listColsSelected;
        UIAddDatasource objDS = null;

        public BindTableData(UIAddDatasource objUIDS)
        {
            InitializeComponent();
            this.objDS = objUIDS;
            listColsSelected = new ArrayList();
            //setColumns("p_pmsGetActiveEmployees");
            //setColumnsSchema(objName);
        }

        public void setTableColumnsSchema(string tableName)
        {
            com.Editors.SchemaDAL obj = new com.Editors.SchemaDAL(DataForm.GetConStr());
            ArrayList listCols = obj.GetTableColumns(tableName);
            int i = 0;
            foreach (GuidDataColumn col in listCols)
            {                 
              addRow(col.ColName.Trim(), i++);
            }
        }

        public void setSPColumns(string spName)
        {
            com.Editors.SchemaDAL obj = new com.Editors.SchemaDAL(DataForm.GetConStr());
            string data = obj.GetStoredProcedureColumns(spName).ToLower();
            int sIndex = data.IndexOf("select") + 6;
            int endIndex = data.IndexOf("from") - sIndex;
            data = data.Substring(sIndex, endIndex);
            string[] arr = data.Split(new char[] { ',' });
            numCols = arr.Length - 1;
            for(int i = 0; i < arr.Length; i++ )
               addRow(arr[i].Trim(), i);
        }

        private void addRow(string colName, int id)
        {
            
            CheckBox chk = new CheckBox();
            chk.Name = "chk"+id;
            chk.Text = colName;

            tableLayoutPanel1.Controls.Add(chk);
            //panel1.Controls.Add(tPanel);
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach(Control ctrl in tableLayoutPanel1.Controls)
            {
                if (ctrl is CheckBox)
                {
                    ((CheckBox)ctrl).Checked = chkAll.Checked;
                }
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in tableLayoutPanel1.Controls)
            {
                if (ctrl is CheckBox)
                {
                    if (((CheckBox)ctrl).Checked)
                        listColsSelected.Add(((CheckBox)ctrl).Text.Trim());
                }
            }
            objDS.BindCols(ListColsSelected);
            //((UIAddDatasource)this.ParentForm).BindCols(ListColsSelected);
            this.Hide();
        }
        
        public ArrayList ListColsSelected
        {
            get { return listColsSelected; }
            set { listColsSelected = value; }
        }

    }
}